/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.api.adwords.starterkit.examples;

import com.google.api.adwords.starterkit.StarterKit;
import com.google.api.adwords.starterkit.settings.AdGroupSettings;
import com.google.api.adwords.starterkit.settings.CampaignSettings;
import com.google.api.adwords.starterkit.settings.KeywordSettings;
import com.google.api.adwords.starterkit.settings.StarterKitSettings;
import com.google.api.adwords.starterkit.settings.TextAdSettings;
import com.google.api.adwords.starterkit.utils.AdTemplate;
import com.google.api.adwords.starterkit.wrappers.AdGroupWrapper;
import com.google.api.adwords.starterkit.wrappers.CampaignWrapper;

/**
 * The Class AdTemplateExample is a demo that uses the templates available at
 * settings.xml file to create TextAds based on specifics rules inside of the 
 * application.
 *  
 * @author rodrigo.f.vale@gmail.com  (Rodrigo Vale)
 */
public class AdTemplateExample {
  public static void howToUse() {
    System.out.print(
        "SYNOPSIS\n" +
        "       AdParamExample [SETTINGS]\n" +
        "\n" +
        "DESCRIPTION\n" +
        "       Simple example that uses the templates available at\n" +
        "settings.xml file to create TextAds based on specifics rules\n" +
        "inside of the application. Keep in mind that AdWords API AdParam\n" +
        "feature updates numeric values in TextAds (like prices) in a\n" +
        "much more efficient way.\n" +
        "EXAMPLE\n" +
        "       java starterkit.jar AdTemplateExample settings.xml\n");
    System.exit(0);
  }
  
  public static void main(String[] args) {
    if (args.length < 1) {
      howToUse();
    }
    StarterKitSettings starterKitSettings =  new StarterKitSettings(args[0]);
    CampaignSettings campaignSettings = 
        starterKitSettings.getCampaignSettings();
    AdGroupSettings adGroupSettings = starterKitSettings.getAdGroupSettings();
    TextAdSettings textAdSettings = starterKitSettings.getTextAdSettings();
    KeywordSettings keywordSettings = starterKitSettings.getKeywordSettings();
        
    StarterKit starterKit = new StarterKit(starterKitSettings);
    starterKit.load();
  
    CampaignWrapper campaignWrapper = 
        starterKit.getCampaignWrapper("Google Next One", campaignSettings);
    AdGroupWrapper adGroupWrapper = 
        campaignWrapper.getAdGroupWrapper("Promotions", adGroupSettings);
    adGroupWrapper.getKeywordWrapper("Next One", false, 
        "http://www.google.com/phone/", keywordSettings);

    for (final AdTemplate template : starterKitSettings.getTextAdSettings()
        .getAdTemplates()) {
      if (template.getTags().contains("price")) {
        String category = "Eletronics";
        template.replaceAll("\\{1\\}", category);
        String shortProductName = "Next One";
        template.replaceAll("\\{2\\}", shortProductName);
        String price = "$499";
        template.replaceAll("\\{3\\}", price);
      } else if (template.getTags().contains("default")) {
        String category = "Eletronics";
        template.replaceAll("\\{1\\}", category);
        String shortProductName = "Next One";
        template.replaceAll("\\{2\\}", shortProductName);
        String discount = "10";
        template.replaceAll("\\{3\\}", discount);
      } else if (template.getTags().contains("noprice")) {
        String category = "Eletronics";
        template.replaceAll("\\{1\\}", category);
        String shortProductName = "Next One";
        template.replaceAll("\\{2\\}", shortProductName);        
      }
      adGroupWrapper.getTextWrapper(template, textAdSettings);
    }    
    starterKit.save();
    starterKit.done();    
  }
}